###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file measures candidates support by agreement x consistency for levels of knowledege and crosspressure
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load("data/data_matrix_scored.Rdata")
candidate_matrix=data_matrix
#candidate_matrix=read.csv("csv/data_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################
############################################################
### Primary Elections (by PID)

####

#candidate_matrix$tr=as.numeric(candidate_matrix$cq==1)
#candidate_matrix$tr[which(candidate_matrix$cq==4)]=NA

#candidate_matrix$ii_scores = candidate_matrix$scores*candidate_matrix$self_place

# include factional endorsement affinity == ff is non-policy identity affinity for groups
# include factional endorsement affinity == ee is policy identity affinity for groups
candidate_matrix$older_white = as.numeric(candidate_matrix$age>=45 & candidate_matrix$white==1)
kk=which(names(candidate_matrix)=='religion')[2]
candidate_matrix$christian = as.numeric(candidate_matrix[,kk]=='Protestant' | candidate_matrix[,kk]=='Roman Catholic' | candidate_matrix[,kk]=='Mormon')
candidate_matrix$militaryhh = as.numeric(candidate_matrix$"milstat_1"=='Yes' | candidate_matrix$"milstat_2"=='Yes' | candidate_matrix$"milstat_3"=='Yes' ) # might be too broad since it includes family
candidate_matrix$female_single = as.numeric(candidate_matrix$female==1 & candidate_matrix$married == 0)
candidate_matrix$black  = candidate_matrix$black
candidate_matrix$hisps = candidate_matrix$hisps
candidate_matrix$unionhh = candidate_matrix$unionhh
candidate_matrix$investor = candidate_matrix$investor


candidate_matrix$ff_reproductive = as.numeric(candidate_matrix$e_reproductive == 1 & candidate_matrix$female_single == 1)
candidate_matrix$ff_civilrights  = as.numeric(candidate_matrix$e_civilrights  == 1 & (candidate_matrix$black == 1 | candidate_matrix$hisps == 1))
candidate_matrix$ff_laborunions  = as.numeric(candidate_matrix$e_laborunions  == 1 & candidate_matrix$unionhh == 1)
candidate_matrix$ff_veterans     = as.numeric(candidate_matrix$e_veterans     == 1 & candidate_matrix$militaryhh == 1)
candidate_matrix$ff_business     = as.numeric(candidate_matrix$e_business     == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_taxreform    = as.numeric(candidate_matrix$e_taxreform    == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_christian    = as.numeric(candidate_matrix$e_christian    == 1 & candidate_matrix$christian == 1)
candidate_matrix$ff_teaparty     = as.numeric(candidate_matrix$e_teaparty     == 1 & candidate_matrix$older_white == 1)

candidate_matrix$ff_energy       = as.numeric(candidate_matrix$e_energy      == 1 & candidate_matrix$mip_env == 1)
candidate_matrix$ff_environment  = as.numeric(candidate_matrix$e_environment == 1 & candidate_matrix$mip_env  == 1)
candidate_matrix$ff_guncontrol   = as.numeric(candidate_matrix$e_guncontrol  == 1 & candidate_matrix$mip_guns == 1)
candidate_matrix$ff_gunrights    = as.numeric(candidate_matrix$e_gunrights   == 1 & candidate_matrix$mip_guns == 1)

vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
vote$pty <- ifelse(vote$conjoints==3,0,1)

########################################################################
########################################################################
# initial descriptives
########################################################################
########################################################################

# d0,r0 minus means that partisans are more likely to take out-party position when issue is less important
# d0,r0 minus means that partisans are more likely to take out-party position when issue is more important


#CC16_301   MIP

#Randomize Rows
#How important are each of these issues to you?
#ROWS:
#a)	Gun control
#b)	Abortion
#c)	Taxes
#d)	Immigration
#e)	Budget Deficit
#f)	Defense Spending
#g)	Social Security
#h)	Environment
#i)	Jobs
#j)	Conflict in Middle East
#k)	National Security
#l)	Race Relations
#m)	Health Care
#n)	Gay Marriage

#COLUMNS:

#1. Very High Importance
#2. Somewhat High Important
#3. Somewhat Low Importance
#4. Very Low Importance
#5. Not Important at All


d0=c()
r0=c()
#D
d0=c(d0,mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==1)]>=0)-mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==1)]<=0)-mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==1)]>=0)-mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==1)]<=0)-mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==1)]>=0)-mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==1)]<=0)-mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==1)]>=0)-mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==1)]<=0)-mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==1)]>=0)-mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==1)]<=0)-mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==1)]>=0)-mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==1)]<=0)-mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==0)]<=0))

#D
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==1)]>0)
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==0)]>0)
#R
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==1)]<0)
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==0)]<0)

#D
d0=c(d0,mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==1)]>=0)-mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==1)]<=0)-mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==0)]<=0))

#D
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==1)]>=0)
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==0)]>=0)
#R
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==1)]<=0)
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==0)]<=0)
#D

d0=c(d0,mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==1)]>=0)-mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==1)]<=0)-mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==1)]>=0)-mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==1)]<=0)-mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==1)]>=0)-mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==1)]<=0)-mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==0)]<=0))

#D
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==1)]>=0)
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==0)]>=0)
#R
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==1)]<=0)
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==0)]<=0)

m0=as.matrix(cbind(d0,r0))
row.names(m0)=c("libcon_envs","libcon_abrt","libcon_guns","libcon_taxs","libcon_immi","libcon_gays", "libcon_crme","libcon_dfns","libcon_need","libcon_trad")
#pty.issue=c(-1,1,-1,-1,1,-1,1,1,-1)


library(xtable)
print(xtable(digits=3,m0[order(m0[,1]-m0[,2]),]),file='appendix/figures/mip_m0_summary.tex')
#d0          r0
#libcon_envs -0.30475978  0.35390332
#libcon_guns -0.15028949  0.18683565
#libcon_gays -0.04473261 -0.01170777
#libcon_taxs -0.02779861 -0.02011921
#libcon_trad  0.05542440 -0.01185632
#libcon_abrt  0.08857830 -0.16167443
#libcon_dfns  0.16118206 -0.12301189
#libcon_need  0.14459664 -0.14813873
#libcon_immi  0.09681687 -0.19795095
#libcon_crme  0.21967294 -0.16813279

#vote$libcon_envs,vote$mip_env
#vote$libcon_abrt,vote$mip_abortion
#vote$libcon_guns,vote$mip_guns
#vote$libcon_taxs,vote$mip_taxes
#vote$libcon_immi,vote$mip_immigration
#vote$libcon_gays,vote$mip_gays
#vote$libcon_crme,vote$mip_crime
#vote$libcon_dfns,vote$mip_natlsecurity
#vote$libcon_need,vote$mip_deficit
#vote$libcon_trad,vote$mip_jobs

d0=c()
r0=c()
#D
d0=rbind(d0,c(mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==1)]>=0),mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==1)]<=0),mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==1)]>=0),mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==1)]<=0),mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==1)]>=0),mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==1)]<=0),mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==1)]>=0),mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==1)]<=0),mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==1)]>=0),mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==1)]<=0),mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==1)]>=0),mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==1)]<=0),mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==0)]<=0)))

#D
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==1)]>0)
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==0)]>0)
#R
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==1)]<0)
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==0)]<0)

#D
d0=rbind(d0,c(mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==1)]>=0),mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==1)]<=0),mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==0)]<=0)))

#D
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==1)]>=0)
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==0)]>=0)
#R
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==1)]<=0)
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==0)]<=0)
#D

d0=rbind(d0,c(mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==1)]>=0),mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==1)]<=0),mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==1)]>=0),mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==1)]<=0),mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==1)]>=0),mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==1)]<=0),mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==0)]<=0)))

#D
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==1)]>=0)
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==0)]>=0)
#R
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==1)]<=0)
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==0)]<=0)

#m0=as.matrix(cbind(d0,r0))
row.names(d0)=row.names(r0)=c("libcon_envs","libcon_abrt","libcon_guns","libcon_taxs","libcon_immi","libcon_gays", "libcon_crme","libcon_dfns","libcon_need","libcon_trad")
cols=c('env','abortion','guns','taxes','immigration','gay marriage','reducing crime','national security/defense','budget deficit','jobs')
#pty.issue=c(-1,1,-1,-1,1,-1,1,1,-1)
d0=as.data.frame(d0)
r0=as.data.frame(r0)

d0=cbind(d0,cols)
r0=cbind(r0,cols)
d0=d0[order(as.numeric(d0[,1])-as.numeric(d0[,2])),]
r0=r0[order(as.numeric(r0[,1])-as.numeric(r0[,2])),]

colnames(d0)=colnames(r0)=c('important','not important','issue frame')
print(xtable(digits=3,d0),file='appendix/figures/mip_dems_summary.tex')
print(xtable(digits=3,r0),file='appendix/figures/mip_reps_summary.tex')

#DEMS: out-party position IF
# important =1, 0
#> d0[order(d0[,1]-d0[,2]),]
#                  [,1]      [,2]
#libcon_envs 0.11064426 0.4154040
#libcon_guns 0.16976950 0.3200590
#libcon_gays 0.15541401 0.2001466
#libcon_taxs 0.09708223 0.1248808
#libcon_trad 0.48998603 0.4345616
#libcon_abrt 0.29973650 0.2111582
#libcon_immi 0.27085533 0.1740385
#libcon_need 0.28881823 0.1442216
#libcon_dfns 0.65050590 0.4893238
#libcon_crme 0.56954887 0.3498759

#REPS: out-party position IF
#> r0[order(r0[,1]-r0[,2]),]
#                 [,1]      [,2]
#libcon_immi 0.2694175 0.4673684 : immigration
#libcon_crme 0.5195865 0.6877193 : crime
#libcon_abrt 0.2911944 0.4528689 : abortion
#libcon_need 0.1922868 0.3404255 : budget => need
#libcon_dfns 0.2330487 0.3560606 : defense
#libcon_taxs 0.4776386 0.4977578 : taxes
#libcon_trad 0.8014484 0.8133047 : jobs => trade
#libcon_gays 0.4085821 0.4202899 : gays
#libcon_guns 0.6868357 0.5000000 : guns
#libcon_envs 0.6827033 0.3288000 : env


########################################################################
########################################################################
# mip stratifications :: series of regressions stratifying on libcon_x<0,>0, etc
########################################################################
########################################################################

#loop over issues to do stratifications

vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
vote$pty <- ifelse(vote$conjoints==3,0,1)

reps <- subset(vote, vote$pid3==1)
dems <- subset(vote, vote$pid3==-1)
inds <- subset(vote, vote$pid3==0)


do.loop.strata=T
if(do.loop.strata==T){

coefL=c('i_co2emissions','i_guncontrol','i_lgbt','i_raisetaxes','i_freetrade','i_righttochoose','i_citizenship','i_need','i_reducemilitary','i_policing')
coefR=c('i_drilling','i_gunrights','i_marriage','i_cuttaxes','i_unfairtrade','i_unbornlives','i_bordersecurity','i_govabuse','i_strengthenmilitary','i_criminals')
lst=c('libcon_envs','libcon_guns','libcon_gays','libcon_taxs','libcon_trad','libcon_abrt','libcon_immi','libcon_need','libcon_dfns','libcon_crme')

repCoefs=list()
demCoefs=list()

for(k in 1:length(lst)){
#dv_choice is the vote choice variable
# pty is included for independents here, otherwise of course NAs out.
ir=which(names(reps)==lst[k])
#attach(reps)
primary_elec_reps_in <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=reps,subset=which(reps[,ir]>0))

primary_elec_reps_clse_in <- clse.f(reps[which(reps[,ir]>0),],primary_elec_reps_in,reps$respondent[which(reps[,ir]>0)])

primary_elec_reps_out <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=reps,subset=which(reps[,ir]<0))

primary_elec_reps_clse_out <- clse.f(reps[which(reps[,ir]<0),],primary_elec_reps_out,reps$respondent[which(reps[,ir]<0)])

c0=c(grep(rownames(primary_elec_reps_clse_in),pattern=coefL[k]),grep(rownames(primary_elec_reps_clse_in),pattern=coefR[k]))
repCoefs[[k]]=list()
if(length(c0)==2){
	repCoefs[[k]][[1]]=primary_elec_reps_clse_in[c0,]
	repCoefs[[k]][[2]]=primary_elec_reps_clse_out[c0,]
} else if(length(c0)==1){
	repCoefs[[k]][[1]]=rbind(rep(NA,4),primary_elec_reps_clse_in[c0,])
	repCoefs[[k]][[2]]=rbind(rep(NA,4),primary_elec_reps_clse_out[c0,])
	rownames(repCoefs[[k]][[1]])=rownames(repCoefs[[k]][[2]])=c(coefL[k],coefR[k])
}

id=which(names(dems)==lst[k])
#attach(dems)
primary_elec_dems_in <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=dems,subset=which(dems[,id]<0))

primary_elec_dems_clse_in <- clse.f(dems[which(dems[,id]<0),],primary_elec_dems_in,dems$respondent[which(dems[,id]<0)])

primary_elec_dems_out <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=dems,subset=which(dems[,id]>0))

primary_elec_dems_clse_out <- clse.f(dems[which(dems[,id]>0),],primary_elec_dems_out,dems$respondent[which(dems[,id]>0)])


c0=c(grep(rownames(primary_elec_dems_clse_in),pattern=coefL[k]),grep(rownames(primary_elec_dems_clse_in),pattern=coefR[k]))
demCoefs[[k]]=list()
if(length(c0)==2){
	demCoefs[[k]][[1]]=primary_elec_dems_clse_in[c0,]
	demCoefs[[k]][[2]]=primary_elec_dems_clse_out[c0,]
} else if(length(c0)==1){
	demCoefs[[k]][[1]]=rbind(primary_elec_dems_clse_in[c0,],rep(NA,4))
	demCoefs[[k]][[2]]=rbind(primary_elec_dems_clse_out[c0,],rep(NA,4))
	rownames(demCoefs[[k]][[1]])=rownames(demCoefs[[k]][[2]])=c(coefL[k],coefR[k])
}
} # loop
} # if-then control

# manually correct trade issues for dems
demCoefs[[5]][[1]][2,]=demCoefs[[5]][[1]][1,]
demCoefs[[5]][[1]][1,]=rep(NA,4)
demCoefs[[5]][[2]][2,]=demCoefs[[5]][[2]][1,]
demCoefs[[5]][[2]][1,]=rep(NA,4)

# build matrix for gplot
k=1
repOut=repCoefs[[k]][[2]]
repIn=repCoefs[[k]][[1]]
demOut=demCoefs[[k]][[2]]
demIn=demCoefs[[k]][[1]]

for(k in 2:10){
	demIn=rbind(demIn,demCoefs[[k]][[1]])
	demOut=rbind(demOut,demCoefs[[k]][[2]])
	repIn=rbind(repIn,repCoefs[[k]][[1]])
	repOut=rbind(repOut,repCoefs[[k]][[2]])
}


labels=c(
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Promote expanding free trade agreements",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect a woman's right to choose",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Reduce the size of military and number of military bases",
"Issue - Strengthen our military and national defense",
"Issue - Reform policing and stop racial profiling",
"Issue - Toughen sentences and penalties for criminals")

demIn=cbind('Consistent Democrat','Democrat',demIn,labels)
demOut=cbind('Inconsistent Democrat','Democrat',demOut,labels)
repIn=cbind('Consistent Republican','Republican',repIn,labels)
repOut=cbind('Inconsistent Republican','Republican',repOut,labels)

rownames(demIn)=rownames(demOut)=as.character(1:nrow(demIn))
rownames(repIn)=rownames(repOut)=as.character(1:nrow(repIn))

colnames(demIn)[1:2]=colnames(demOut)[1:2]=c('Consistency','PID')
colnames(repIn)[1:2]=colnames(repOut)[1:2]=c('Consistency','PID')

reOrder=c(19,1,3,5,7,11,13,15,17,9,c(19,1,3,5,7,11,13,15,17,9)+1)
demIn[9,3:4]=0
demOut[9,3:4]=0
repIn[9,3:4]=0
repOut[9,3:4]=0

ggplot_stacked = rbind(demIn[reOrder,],demOut[reOrder,],repIn[reOrder,],repOut[reOrder,])[,c(1:4,7)]



colnames(ggplot_stacked)=c('Consistency',"PID","estimate","se","labels")
ggplot_stacked=as.data.frame(ggplot_stacked)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)


pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

#ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
#ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))

pdf("appendix/figures/mip_issue_stratification_choice.pdf", width=9, height=7, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Consistency)) +
    scale_color_manual(values = c("blue4","firebrick1","lightsteelblue3","rosybrown")) +
    scale_x_discrete(limits=rev(labels)) +
facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.2,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 8)) +
#    geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=10.5,size=0.5,linetype="dashed",col='darkgrey') +
	#geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

# END LOOP STRATIFICATION





###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load("data/data_matrix_scored.Rdata")
candidate_matrix=data_matrix
#candidate_matrix=read.csv("csv/data_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################
############################################################
### Primary Elections (by PID)

####

#candidate_matrix$tr=as.numeric(candidate_matrix$cq==1)
#candidate_matrix$tr[which(candidate_matrix$cq==4)]=NA

#candidate_matrix$ii_scores = candidate_matrix$scores*candidate_matrix$self_place

# include factional endorsement affinity == ff is non-policy identity affinity for groups
# include factional endorsement affinity == ee is policy identity affinity for groups
candidate_matrix$older_white = as.numeric(candidate_matrix$age>=45 & candidate_matrix$white==1)
kk=which(names(candidate_matrix)=='religion')[2]
candidate_matrix$christian = as.numeric(candidate_matrix[,kk]=='Protestant' | candidate_matrix[,kk]=='Roman Catholic' | candidate_matrix[,kk]=='Mormon')
candidate_matrix$militaryhh = as.numeric(candidate_matrix$"milstat_1"=='Yes' | candidate_matrix$"milstat_2"=='Yes' | candidate_matrix$"milstat_3"=='Yes' ) # might be too broad since it includes family
candidate_matrix$female_single = as.numeric(candidate_matrix$female==1 & candidate_matrix$married == 0)
candidate_matrix$black  = candidate_matrix$black
candidate_matrix$hisps = candidate_matrix$hisps
candidate_matrix$unionhh = candidate_matrix$unionhh
candidate_matrix$investor = candidate_matrix$investor


candidate_matrix$ff_reproductive = as.numeric(candidate_matrix$e_reproductive == 1 & candidate_matrix$female_single == 1)
candidate_matrix$ff_civilrights  = as.numeric(candidate_matrix$e_civilrights  == 1 & (candidate_matrix$black == 1 | candidate_matrix$hisps == 1))
candidate_matrix$ff_laborunions  = as.numeric(candidate_matrix$e_laborunions  == 1 & candidate_matrix$unionhh == 1)
candidate_matrix$ff_veterans     = as.numeric(candidate_matrix$e_veterans     == 1 & candidate_matrix$militaryhh == 1)
candidate_matrix$ff_business     = as.numeric(candidate_matrix$e_business     == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_taxreform    = as.numeric(candidate_matrix$e_taxreform    == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_christian    = as.numeric(candidate_matrix$e_christian    == 1 & candidate_matrix$christian == 1)
candidate_matrix$ff_teaparty     = as.numeric(candidate_matrix$e_teaparty     == 1 & candidate_matrix$older_white == 1)

candidate_matrix$ff_energy       = as.numeric(candidate_matrix$e_energy      == 1 & candidate_matrix$mip_env == 1)
candidate_matrix$ff_environment  = as.numeric(candidate_matrix$e_environment == 1 & candidate_matrix$mip_env  == 1)
candidate_matrix$ff_guncontrol   = as.numeric(candidate_matrix$e_guncontrol  == 1 & candidate_matrix$mip_guns == 1)
candidate_matrix$ff_gunrights    = as.numeric(candidate_matrix$e_gunrights   == 1 & candidate_matrix$mip_guns == 1)

vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
vote$pty <- ifelse(vote$conjoints==3,0,1)

########################################################################
########################################################################
# initial descriptives
########################################################################
########################################################################

# d0,r0 minus means that partisans are more likely to take out-party position when issue is less important
# d0,r0 minus means that partisans are more likely to take out-party position when issue is more important


#CC16_301   MIP

#Randomize Rows
#How important are each of these issues to you?
#ROWS:
#a)	Gun control
#b)	Abortion
#c)	Taxes
#d)	Immigration
#e)	Budget Deficit
#f)	Defense Spending
#g)	Social Security
#h)	Environment
#i)	Jobs
#j)	Conflict in Middle East
#k)	National Security
#l)	Race Relations
#m)	Health Care
#n)	Gay Marriage

#COLUMNS:

#1. Very High Importance
#2. Somewhat High Important
#3. Somewhat Low Importance
#4. Very Low Importance
#5. Not Important at All


d0=c()
r0=c()
#D
d0=c(d0,mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==1)]>=0)-mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==1)]<=0)-mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==1)]>=0)-mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==1)]<=0)-mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==1)]>=0)-mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==1)]<=0)-mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==1)]>=0)-mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==1)]<=0)-mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==1)]>=0)-mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==1)]<=0)-mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==1)]>=0)-mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==1)]<=0)-mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==0)]<=0))

#D
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==1)]>0)
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==0)]>0)
#R
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==1)]<0)
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==0)]<0)

#D
d0=c(d0,mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==1)]>=0)-mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==1)]<=0)-mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==0)]<=0))

#D
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==1)]>=0)
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==0)]>=0)
#R
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==1)]<=0)
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==0)]<=0)
#D

d0=c(d0,mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==1)]>=0)-mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==1)]<=0)-mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==1)]>=0)-mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==1)]<=0)-mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==0)]<=0))

#D
d0=c(d0,mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==1)]>=0)-mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==0)]>=0))
#R
r0=c(r0,mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==1)]<=0)-mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==0)]<=0))

#D
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==1)]>=0)
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==0)]>=0)
#R
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==1)]<=0)
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==0)]<=0)

m0=as.matrix(cbind(d0,r0))
row.names(m0)=c("libcon_envs","libcon_abrt","libcon_guns","libcon_taxs","libcon_immi","libcon_gays", "libcon_crme","libcon_dfns","libcon_need","libcon_trad")
#pty.issue=c(-1,1,-1,-1,1,-1,1,1,-1)


library(xtable)
print(xtable(digits=3,m0[order(m0[,1]-m0[,2]),]),file='appendix/figures/mip_m0_summary.tex')
#d0          r0
#libcon_envs -0.30475978  0.35390332
#libcon_guns -0.15028949  0.18683565
#libcon_gays -0.04473261 -0.01170777
#libcon_taxs -0.02779861 -0.02011921
#libcon_trad  0.05542440 -0.01185632
#libcon_abrt  0.08857830 -0.16167443
#libcon_dfns  0.16118206 -0.12301189
#libcon_need  0.14459664 -0.14813873
#libcon_immi  0.09681687 -0.19795095
#libcon_crme  0.21967294 -0.16813279

#vote$libcon_envs,vote$mip_env
#vote$libcon_abrt,vote$mip_abortion
#vote$libcon_guns,vote$mip_guns
#vote$libcon_taxs,vote$mip_taxes
#vote$libcon_immi,vote$mip_immigration
#vote$libcon_gays,vote$mip_gays
#vote$libcon_crme,vote$mip_crime
#vote$libcon_dfns,vote$mip_natlsecurity
#vote$libcon_need,vote$mip_deficit
#vote$libcon_trad,vote$mip_jobs

d0=c()
r0=c()
#D
d0=rbind(d0,c(mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==1)]>=0),mean(vote$libcon_envs[which(vote$pid3==-1 & vote$mip_env==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==1)]<=0),mean(vote$libcon_envs[which(vote$pid3==1 & vote$mip_env==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==1)]>=0),mean(vote$libcon_abrt[which(vote$pid3==-1 & vote$mip_abortion==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==1)]<=0),mean(vote$libcon_abrt[which(vote$pid3==1 & vote$mip_abortion==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==1)]>=0),mean(vote$libcon_guns[which(vote$pid3==-1 & vote$mip_guns==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==1)]<=0),mean(vote$libcon_guns[which(vote$pid3==1 & vote$mip_guns==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==1)]>=0),mean(vote$libcon_taxs[which(vote$pid3==-1 & vote$mip_taxes==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==1)]<=0),mean(vote$libcon_taxs[which(vote$pid3==1 & vote$mip_taxes==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==1)]>=0),mean(vote$libcon_immi[which(vote$pid3==-1 & vote$mip_immigration==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==1)]<=0),mean(vote$libcon_immi[which(vote$pid3==1 & vote$mip_immigration==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==1)]>=0),mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_gays==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==1)]<=0),mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_gays==0)]<=0)))

#D
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==1)]>0)
#mean(vote$libcon_gays[which(vote$pid3==-1 & vote$mip_religion==0)]>0)
#R
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==1)]<0)
#mean(vote$libcon_gays[which(vote$pid3==1 & vote$mip_religion==0)]<0)

#D
d0=rbind(d0,c(mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==1)]>=0),mean(vote$libcon_crme[which(vote$pid3==-1 & vote$mip_crime==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==1)]<=0),mean(vote$libcon_crme[which(vote$pid3==1 & vote$mip_crime==0)]<=0)))

#D
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==1)]>=0)
#mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_defense==0)]>=0)
#R
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==1)]<=0)
#mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_defense==0)]<=0)
#D

d0=rbind(d0,c(mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==1)]>=0),mean(vote$libcon_dfns[which(vote$pid3==-1 & vote$mip_natlsecurity==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==1)]<=0),mean(vote$libcon_dfns[which(vote$pid3==1 & vote$mip_natlsecurity==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==1)]>=0),mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_deficit==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==1)]<=0),mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_deficit==0)]<=0)))

#D
d0=rbind(d0,c(mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==1)]>=0),mean(vote$libcon_trad[which(vote$pid3==-1 & vote$mip_jobs==0)]>=0)))
#R
r0=rbind(r0,c(mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==1)]<=0),mean(vote$libcon_trad[which(vote$pid3==1 & vote$mip_jobs==0)]<=0)))

#D
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==1)]>=0)
#mean(vote$libcon_need[which(vote$pid3==-1 & vote$mip_socsec==0)]>=0)
#R
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==1)]<=0)
#mean(vote$libcon_need[which(vote$pid3==1 & vote$mip_socsec==0)]<=0)

#m0=as.matrix(cbind(d0,r0))
row.names(d0)=row.names(r0)=c("libcon_envs","libcon_abrt","libcon_guns","libcon_taxs","libcon_immi","libcon_gays", "libcon_crme","libcon_dfns","libcon_need","libcon_trad")
cols=c('env','abortion','guns','taxes','immigration','gay marriage','reducing crime','national security/defense','budget deficit','jobs')
#pty.issue=c(-1,1,-1,-1,1,-1,1,1,-1)
d0=as.data.frame(d0)
r0=as.data.frame(r0)

d0=cbind(d0,cols)
r0=cbind(r0,cols)
d0=d0[order(as.numeric(d0[,1])-as.numeric(d0[,2])),]
r0=r0[order(as.numeric(r0[,1])-as.numeric(r0[,2])),]

colnames(d0)=colnames(r0)=c('important','not important','issue frame')
print(xtable(digits=3,d0),file='appendix/figures/mip_dems_summary.tex')
print(xtable(digits=3,r0),file='appendix/figures/mip_reps_summary.tex')

#DEMS: out-party position IF
# important =1, 0
#> d0[order(d0[,1]-d0[,2]),]
#                  [,1]      [,2]
#libcon_envs 0.11064426 0.4154040
#libcon_guns 0.16976950 0.3200590
#libcon_gays 0.15541401 0.2001466
#libcon_taxs 0.09708223 0.1248808
#libcon_trad 0.48998603 0.4345616
#libcon_abrt 0.29973650 0.2111582
#libcon_immi 0.27085533 0.1740385
#libcon_need 0.28881823 0.1442216
#libcon_dfns 0.65050590 0.4893238
#libcon_crme 0.56954887 0.3498759

#REPS: out-party position IF
#> r0[order(r0[,1]-r0[,2]),]
#                 [,1]      [,2]
#libcon_immi 0.2694175 0.4673684 : immigration
#libcon_crme 0.5195865 0.6877193 : crime
#libcon_abrt 0.2911944 0.4528689 : abortion
#libcon_need 0.1922868 0.3404255 : budget => need
#libcon_dfns 0.2330487 0.3560606 : defense
#libcon_taxs 0.4776386 0.4977578 : taxes
#libcon_trad 0.8014484 0.8133047 : jobs => trade
#libcon_gays 0.4085821 0.4202899 : gays
#libcon_guns 0.6868357 0.5000000 : guns
#libcon_envs 0.6827033 0.3288000 : env


########################################################################
########################################################################
# mip stratifications :: series of regressions stratifying on libcon_x<0,>0, etc
########################################################################
########################################################################

#loop over issues to do stratifications

vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
vote$pty <- ifelse(vote$conjoints==3,0,1)

reps <- subset(vote, vote$pid3==1)
dems <- subset(vote, vote$pid3==-1)
inds <- subset(vote, vote$pid3==0)


do.loop.strata=T
if(do.loop.strata==T){

coefL=c('i_co2emissions','i_guncontrol','i_lgbt','i_raisetaxes','i_freetrade','i_righttochoose','i_citizenship','i_need','i_reducemilitary','i_policing')
coefR=c('i_drilling','i_gunrights','i_marriage','i_cuttaxes','i_unfairtrade','i_unbornlives','i_bordersecurity','i_govabuse','i_strengthenmilitary','i_criminals')
#lst=c('libcon_envs','libcon_guns','libcon_gays','libcon_taxs','libcon_trad','libcon_abrt','libcon_immi','libcon_need','libcon_dfns','libcon_crme')
lst=c('mip_env','mip_guns','mip_gays','mip_taxes','mip_jobs','mip_abortion','mip_immigration','mip_deficit','mip_natlsecurity','mip_crime')

#vote$libcon_envs,vote$mip_env
#vote$libcon_abrt,vote$mip_abortion
#vote$libcon_guns,vote$mip_guns
#vote$libcon_taxs,vote$mip_taxes
#vote$libcon_immi,vote$mip_immigration
#vote$libcon_gays,vote$mip_gays
#vote$libcon_crme,vote$mip_crime
#vote$libcon_dfns,vote$mip_natlsecurity
#vote$libcon_need,vote$mip_deficit
#vote$libcon_trad,vote$mip_jobs

repCoefs=list()
demCoefs=list()

for(k in 1:length(lst)){
#dv_choice is the vote choice variable
# pty is included for independents here, otherwise of course NAs out.
ir=which(names(reps)==lst[k])
#attach(reps)
primary_elec_reps_in <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=reps,subset=which(reps[,ir]==1))

primary_elec_reps_clse_in <- clse.f(reps[which(reps[,ir]==1),],primary_elec_reps_in,reps$respondent[which(reps[,ir]==1)])

primary_elec_reps_out <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=reps,subset=which(reps[,ir]==0))

primary_elec_reps_clse_out <- clse.f(reps[which(reps[,ir]==0),],primary_elec_reps_out,reps$respondent[which(reps[,ir]==0)])

c0=c(grep(rownames(primary_elec_reps_clse_in),pattern=coefL[k]),grep(rownames(primary_elec_reps_clse_in),pattern=coefR[k]))
repCoefs[[k]]=list()
if(length(c0)==2){
	repCoefs[[k]][[1]]=primary_elec_reps_clse_in[c0,]
	repCoefs[[k]][[2]]=primary_elec_reps_clse_out[c0,]
} else if(length(c0)==1){
	repCoefs[[k]][[1]]=rbind(rep(NA,4),primary_elec_reps_clse_in[c0,])
	repCoefs[[k]][[2]]=rbind(rep(NA,4),primary_elec_reps_clse_out[c0,])
	rownames(repCoefs[[k]][[1]])=rownames(repCoefs[[k]][[2]])=c(coefL[k],coefR[k])
}

id=which(names(dems)==lst[k])
#attach(dems)
primary_elec_dems_in <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=dems,subset=which(dems[,id]==1))

primary_elec_dems_clse_in <- clse.f(dems[which(dems[,id]==1),],primary_elec_dems_in,dems$respondent[which(dems[,id]==1)])

primary_elec_dems_out <- lm(dv_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,data=dems,subset=which(dems[,id]==0))

primary_elec_dems_clse_out <- clse.f(dems[which(dems[,id]==0),],primary_elec_dems_out,dems$respondent[which(dems[,id]==0)])


c0=c(grep(rownames(primary_elec_dems_clse_in),pattern=coefL[k]),grep(rownames(primary_elec_dems_clse_in),pattern=coefR[k]))
demCoefs[[k]]=list()
if(length(c0)==2){
	demCoefs[[k]][[1]]=primary_elec_dems_clse_in[c0,]
	demCoefs[[k]][[2]]=primary_elec_dems_clse_out[c0,]
} else if(length(c0)==1){
	demCoefs[[k]][[1]]=rbind(primary_elec_dems_clse_in[c0,],rep(NA,4))
	demCoefs[[k]][[2]]=rbind(primary_elec_dems_clse_out[c0,],rep(NA,4))
	rownames(demCoefs[[k]][[1]])=rownames(demCoefs[[k]][[2]])=c(coefL[k],coefR[k])
}
} # loop
} # if-then control

# manually correct trade issues for dems
demCoefs[[5]][[1]][2,]=demCoefs[[5]][[1]][1,]
demCoefs[[5]][[1]][1,]=rep(NA,4)
demCoefs[[5]][[2]][2,]=demCoefs[[5]][[2]][1,]
demCoefs[[5]][[2]][1,]=rep(NA,4)

# build matrix for gplot
k=1
repOut=repCoefs[[k]][[2]]
repIn=repCoefs[[k]][[1]]
demOut=demCoefs[[k]][[2]]
demIn=demCoefs[[k]][[1]]

for(k in 2:10){
	demIn=rbind(demIn,demCoefs[[k]][[1]])
	demOut=rbind(demOut,demCoefs[[k]][[2]])
	repIn=rbind(repIn,repCoefs[[k]][[1]])
	repOut=rbind(repOut,repCoefs[[k]][[2]])
}


labels=c(
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Promote expanding free trade agreements",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect a woman's right to choose",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Reduce the size of military and number of military bases",
"Issue - Strengthen our military and national defense",
"Issue - Reform policing and stop racial profiling",
"Issue - Toughen sentences and penalties for criminals")

demIn=cbind('Important Democrat','Democrat',demIn,labels)
demOut=cbind('Unimportant Democrat','Democrat',demOut,labels)
repIn=cbind('Important Republican','Republican',repIn,labels)
repOut=cbind('Unimportant Republican','Republican',repOut,labels)

rownames(demIn)=rownames(demOut)=as.character(1:nrow(demIn))
rownames(repIn)=rownames(repOut)=as.character(1:nrow(repIn))

colnames(demIn)[1:2]=colnames(demOut)[1:2]=c('Importance','PID')
colnames(repIn)[1:2]=colnames(repOut)[1:2]=c('Importance','PID')

reOrder=c(19,1,3,5,7,11,13,15,17,9,c(19,1,3,5,7,11,13,15,17,9)+1)
demIn[9,3:4]=0
demOut[9,3:4]=0
repIn[9,3:4]=0
repOut[9,3:4]=0

ggplot_stacked = rbind(demIn[reOrder,],demOut[reOrder,],repIn[reOrder,],repOut[reOrder,])[,c(1:4,7)]



colnames(ggplot_stacked)=c('Importance',"PID","estimate","se","labels")
ggplot_stacked=as.data.frame(ggplot_stacked)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)


pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

#ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
#ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))

pdf("appendix/figures/mip_issue_stratification_choice_by_importance.pdf", width=9, height=7, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Importance)) +
    scale_color_manual(values = c("blue4","firebrick1","lightsteelblue3","rosybrown")) +
    scale_x_discrete(limits=rev(labels)) +
facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.2,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 8)) +
#    geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=10.5,size=0.5,linetype="dashed",col='darkgrey') +
	#geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

# END LOOP STRATIFICATION
